package com.zy.cat.common.config;

import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

@Component
public class AsyncThreadPoolConfig {
	/**
	 * @info 配置异步线程池
	 * @return
	 */
	@Bean(name = "zyasyncPoolTaskExecutor")
	public ThreadPoolTaskExecutor getAsyncThreadPoolTaskExecutor() {
	    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
	    //线程池维护线程的最少数量
	    taskExecutor.setCorePoolSize(20);
	    //线程池维护线程的最大数量,只有在缓冲队列满了之后才会申请超过核心线程数的线程
	    taskExecutor.setMaxPoolSize(200);
	    //缓存队列
	    taskExecutor.setQueueCapacity(25);
	    //允许的空闲时间,当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
	    taskExecutor.setKeepAliveSeconds(200);
	    taskExecutor.setThreadNamePrefix("zyAsync-");
	    // 线程池对拒绝任务（无线程可用）的处理策略，目前只支持AbortPolicy、CallerRunsPolicy；默认为后者
	    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
	    //调度器shutdown被调用时等待当前被调度的任务完成
	    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
	    //等待时长
	    taskExecutor.setAwaitTerminationSeconds(60);
	    taskExecutor.initialize();
	    return taskExecutor;
	}
}
